package com.baicaoyuan.web.service;

import com.baicaoyuan.web.model.CrowdfundingUserDO;
import com.baicaoyuan.web.repository.CrowdfundingUserRepository;
import com.baicaoyuan.web.request.br.BCrowdfundingIdRequest;
import com.baicaoyuan.web.request.br.BCrowdfundingUserRequest;
import com.baicaoyuan.web.response.CrowdfundingUserResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 功能描述：
 *
 * @author 罗富祥
 * 创建时间： 2018/1/4
 */
@Service
public class CrowdfundingUserService {

    @Autowired
    CrowdfundingUserRepository crowdfundingUserRepository;

    public void save(BCrowdfundingUserRequest request) {
        Objects.requireNonNull(request);
        CrowdfundingUserDO c = new CrowdfundingUserDO();
        BeanUtils.copyProperties(request, c);
        this.crowdfundingUserRepository.save(c);
    }

    public List<CrowdfundingUserResponse> getUsers(BCrowdfundingIdRequest request) {
        List<CrowdfundingUserDO> list = this.crowdfundingUserRepository.findByCrowdfundingIdAndStatusIsOrderByCreateTimeDesc(request.getId(), "1");
        List<CrowdfundingUserResponse> result = new ArrayList<>();
        list.forEach(x -> {
            CrowdfundingUserResponse r = new CrowdfundingUserResponse();
            BeanUtils.copyProperties(x, r);
            result.add(r);
        });

        return result;

    }
}

